//
//  VKSuggestionController.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKSuggestionController.h"
#import "VKHisRecordController.h"
@interface VKSuggestionController ()<UITextViewDelegate>
@property (nonatomic, strong)UITextView *textView;
@property (nonatomic, strong)UITextField *iphoneTf;
@property (nonatomic, strong)UITextField *wechatTf;
@property (nonatomic, strong)UIView *bgView;

@end

@implementation VKSuggestionController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"意见反馈";
    [self setupRightItem];
    
    [self setupUI];
}
- (void)setupUI{
    UIView *bgView = [[UIView alloc]init];
    [self.view addSubview:bgView];
    _bgView = bgView;
    bgView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(@0);
    }];
    
    self.textView = [[UITextView alloc]init];
//    self.textView.layer.cornerRadius = 5;
//    self.textView.layer.masksToBounds = YES;
    self.textView.placeholder = @"请输入内容...";
    self.textView.placeholderColor = [UIColor cz_colorWithHex:0xFFB2B3B7];
    [bgView addSubview:_textView];
    self.textView.font = [UIFont systemFontOfSize:17];
    self.textView.delegate = self;
    self.textView.backgroundColor = [UIColor whiteColor];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.right.equalTo(@(-12));
        make.top.equalTo(@12);
        make.height.equalTo(@176);
    }];
    
    
    UIView *iphoneView = [[UIView alloc]init];
    [bgView addSubview:iphoneView];
    iphoneView.backgroundColor = [UIColor whiteColor];
    [iphoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textView.mas_bottom).offset(25);
        make.left.equalTo(@12);
        make.right.mas_equalTo(-12);
        make.height.equalTo(@52);
    }];
    
    _iphoneTf = [[UITextField alloc]init];
    _iphoneTf.backgroundColor = [UIColor whiteColor];
    _iphoneTf.placeholder = @"请输入您的联系方式";
    [iphoneView addSubview:_iphoneTf];
    [_iphoneTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textView.mas_bottom).offset(25);
        make.left.equalTo(iphoneView).offset(6);
        make.right.mas_equalTo(iphoneView).offset(-6);
        make.height.equalTo(@52);
    }];
    
    UIView *wechatView = [[UIView alloc]init];
    wechatView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:wechatView];
    [wechatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iphoneView.mas_bottom).offset(12);
        make.left.equalTo(@12);
        make.right.mas_equalTo(-12);
        make.height.equalTo(@52);
    }];
    _wechatTf = [[UITextField alloc]init];
    _wechatTf.backgroundColor = [UIColor whiteColor];
    _wechatTf.placeholder = @"请输入您的微信号";
    [wechatView addSubview:_wechatTf];
    [_wechatTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iphoneView.mas_bottom).offset(12);
        make.left.equalTo(wechatView).offset(6);
        make.right.mas_equalTo(wechatView).offset(-6);
        make.height.equalTo(@52);
    }];
    
    UIButton *commitBtn = [[UIButton alloc]init];
    [bgView addSubview:commitBtn];
    [commitBtn setImage:[UIImage imageNamed:@"vkbtn"] forState:UIControlStateNormal];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_wechatTf.mas_bottom).offset(50);
        make.left.equalTo(@15);
        make.width.equalTo(@([UIScreen cz_screenWidth] - 30));
        make.height.equalTo(@55);
    }];
    commitBtn.imageView.contentMode = UIViewContentModeScaleToFill;
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *commitLabel = [[UILabel alloc]init];
    [bgView addSubview:commitLabel];
    commitLabel.textColor = [UIColor whiteColor];
    commitLabel.font = [UIFont systemFontOfSize:16];
    commitLabel.text = @"提交";
    [commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(commitBtn).offset(15);
        make.centerX.equalTo(commitBtn);
    }];
    
}
- (void)commitBtnClick{
    
    NSString *content = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *tel = [_iphoneTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *wechat = [_wechatTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (content.length == 0 || [content isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"请输入内容" toView:self.view];
        return;
    }
      
    if (wechat.length == 0 && tel.length == 0) {
        [MBProgressHUD showSuccess:@"请输入手机号或微信号" toView:self.view];
        return;
    }
    
//    if (tel.length == 0 || [tel isEqualToString:@""]) {
//        [MBProgressHUD showSuccess:@"请输入您的联系方式" toView:self.view];
//        return;
//    }
    if (!(tel.length == 0 || [tel isEqualToString:@""])) {
        if (![self checkTelNumber:tel]) {
            [MBProgressHUD showSuccess:@"手机号格式不正确" toView:self.view];
            return;
        }
    }
    
//    if (wechat.length == 0 || [wechat isEqualToString:@""]) {
//        [MBProgressHUD showSuccess:@"请输入您的微信号" toView:self.view];
//        return;
//    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在提交...";
    [hud show:YES];
    NSMutableDictionary *pagramsD = @{@"token":[UserModel getUserModel].token,@"opinionContent":content,@"tel":tel,@"wxNum":wechat}.mutableCopy;
    [WYHXNetWorkService netWorkSerViceRequestWithPostUrl:WKMeCommitOptionRecord withPostParams:pagramsD withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            [MBProgressHUD showSuccess:@"提交成功" toView:self.view];
            self.textView.text = @"";
            self.iphoneTf.text = @"";
            self.wechatTf.text = @"";
            [self.bgView removeFromSuperview];
            [self setupCommit];
        }else{
            [MBProgressHUD showSuccess:@"提交失败" toView:self.view];
        }
    }];
}
- (void)setupCommit{
    // 实名认证信息已提交
    UIImageView *cerImageV = [[UIImageView alloc]init];
    [self.view addSubview:cerImageV];
    cerImageV.image = [UIImage imageNamed:@"success"];
    [cerImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
        make.height.equalTo(@110);
        make.width.equalTo(@140);
    }];
    
    UILabel *cerLabel = [[UILabel alloc]init];
    [self.view addSubview:cerLabel];
    cerLabel.text = @"意见反馈已提交,\n请耐心等待客服与您联系!";
    cerLabel.numberOfLines = 0;
    cerLabel.font = [UIFont systemFontOfSize:16];
    cerLabel.textAlignment = NSTextAlignmentCenter;
    cerLabel.textColor = [UIColor cz_colorWithRed:237 green:100 blue:52];
    [cerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(cerImageV.mas_bottom).offset(20);
        make.left.right.mas_equalTo(self.view);
    }];
}
- (BOOL)checkTelNumber:(NSString *)elNumber{
    NSString *pattern =@"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:elNumber];
    
    return isMatch;
}
// 右边Item
- (void)setupRightItem{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"历史记录" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.frame = CGRectMake(0, 0, 70*kScaleFit, 30*kScaleFit);
    [button setTitleColor:[UIColor cz_colorWithRed:40 green:40 blue:40] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button] ;
//    [button mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@30*kScaleFit);
//        make.width.equalTo(@70*kScaleFit);
//    }];
    [button addTarget:self action:@selector(buttonItemClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonItemClick{
    [self.navigationController pushViewController:[[VKHisRecordController alloc]init] animated:YES];
}
@end
